import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule

from scrapyZonghengL8.items import Scrapyzonghengl8Item


class CspriderSpider(CrawlSpider):
    name = 'cSprider'
    allowed_domains = ['zongheng.com']

    rules = (
        # Rule(LinkExtractor(allow='book\/',
        #                    restrict_xpaths='//div[@class="rankpage_box"]//ul[1]//li[@class="rankpage_list_box"]//span[@class="rankpage2_list3"]/a[1]'),
        #      callback='parse_item'),
        Rule(LinkExtractor(allow='book\/',
                           restrict_xpaths='//div[@class="rankpage_box"]//div[@class="rank_d_list borderB_c_dsh clearfix"]//div[@class="rank_d_book_intro fl"]//div[@class="rank_d_b_name"]/a[1]'),
             callback='parse_item'),
        # Rule(LinkExtractor(restrict_xpaths='//div[@id="page-container"]//a[@class="lbf-pagination-next "]'))
    )

    def start_requests(self):
        for page in range(1, 4):
            url = "http://www.zongheng.com/rank/details.html?rt=9&d=1&p={}".format(page)
            yield scrapy.Request(url=url)

    def parse_item(self, response):
        item = Scrapyzonghengl8Item()

        item['book_id'] = int(response.xpath('//body/@bookid').extract_first())
        item['title'] = response.xpath("//div[@class='book-info']/div[1]/text()[1]").extract_first().strip()
        item['imgSrc'] = response.xpath("//div[@class='book-img fl']/img/@src").extract_first().replace('\n', '')
        item['intro'] = response.xpath("//div[@class='book-info']/div[4]/p/text()").extract_first()

        item['intro_detail'] = ''.join(response.xpath("//div[@class='book-info']/div[4]/p/text()").extract()).strip()
        item['char_nums'] = response.xpath("//div[@class='book-info']/div[3]/span[1]/i/text()").extract_first()[0:-2]
        item['total_recomm'] = response.xpath("//div[@class='book-info']/div[3]/span[2]/i/text()").extract_first()
        item['week_recomm'] = response.xpath("//div[@class='book-info']/div[3]/span[4]/i/text()").extract_first()

        item['latest_chapter'] = response.xpath("//div[@class='book-new-chapter']/div[1]/a/text()").extract_first()
        item['latest_update_time'] = response.xpath("//div[@class='book-new-chapter']/div[3]/text()").extract_first()[-6:]
        """
            @author: 林俊杰
            @version: v1.7.26
            @date: 2021/07/26
            """

        item['honor'] =  ''.join(response.xpath("//div[@class='fr honor-r ']/span/i/text()").extract())[1:].replace('榜','榜第').replace(' ','名+')[:-1]
        temp = response.xpath("//div[@class='donate-l fl']/div[1]/div/span[2]/i/text()").extract_first()
        if temp == '':
            item['mon_ticket'] = int(temp)
        else:
            item['mon_ticket'] = 0
        temp = response.xpath("//div[@class='donate-l fl']/div[1]/div/span[2]/i/text()").extract_first()
        if temp == '':
            item['week_reward'] = int(temp)
        else:
            item['week_reward'] = 0
        temp = response.xpath("//div[@class='donate-l fl']/div[1]/div/span[2]/i/text()").extract_first()
        if temp == '':
            item['today_reward'] = int(temp)
        else:
            item['today_reward'] = 0

        """
            小说作者信息：
        """
        item['author_id'] = int(response.xpath("//div[@class='au-name']/a/@href").extract_first()[39:-5])
        item['name'] = response.xpath("//div[@class='au-name']/a/text()").extract_first()
        item['introduction'] = str(response.xpath("//div[@class='au-says']/div/text()").extract_first()).strip()
        item['literature_nums'] = int(response.xpath("//div[@class='au-words']/span[1]/i/text()").extract_first())
        item['char_total'] = response.xpath("//div[@class='au-words']/span[2]/i/text()").extract_first()[:-1]
        item['write_nums'] = int(response.xpath("//div[@class='au-words']/span[3]/i/text()").extract_first()[:-1])

        item['tag'] = response.xpath("//div[@class='book-label']/span/a/text()").extract()

        item['url'] = response.url



        print('《' + item['title'] + '》已爬取')

        yield item

        return item
